<?xml version="1.0" encoding="UTF-8"?>
<!--
    Document   : fleda.xslt
    Created on : 28. duben 2011, 17:50
    Author     : newohybat newohybat@gmail.com
    Description:
        Stylesheet to transform source data(xhtml) to internal representation (xml - internalConcerts)
-->

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://code.google.com/p/kalkon/internalConcerts">

    <xsl:output method="xml"
                encoding="UTF-8"
                indent="yes"
                media-type="text/xml"/>

<!-- Forms VCALENDAR -->
    <xsl:template match="/html">
        <concerts>
            <xsl:attribute name="id">fleda</xsl:attribute>
            <xsl:apply-templates select="//tbody/tr[td[2][child::text()]]"/>
        </concerts>
    </xsl:template>

<!--  Template to form each event  -->
    <xsl:template match="//tbody/tr">
        <concert>
            <title><xsl:value-of select="td[4][a[child::text()]]"/></title>
            <description><xsl:value-of select="td[5]"/></description>
            <location>Klub Fléda,Štefánikova 24,Brno</location>
            <day_start><xsl:apply-templates select="//h1"/><xsl:apply-templates select="td[1]"/></day_start>
            <time_start><xsl:value-of select="concat(substring-before(td[2],':'),substring-after(td[2],':'),'00')"/></time_start>
        </concert>
    </xsl:template>

<!--  Gets year and month of the year  -->
    <xsl:template match="//h1">
        <xsl:variable name="datePart" select="substring-after(substring-after(substring-after(.,' '),' '),' ')"/>
        <xsl:value-of select="substring-after($datePart,' ')"/>
        <xsl:choose>
            <xsl:when test="contains(.,'leden')"><xsl:text>01</xsl:text></xsl:when>
            <xsl:when test="contains(.,'únor')"><xsl:text>02</xsl:text></xsl:when>
            <xsl:when test="contains(.,'březen')"><xsl:text>03</xsl:text></xsl:when>
            <xsl:when test="contains(.,'duben')"><xsl:text>04</xsl:text></xsl:when>
            <xsl:when test="contains(.,'květen')"><xsl:text>05</xsl:text></xsl:when>
            <xsl:when test="contains(.,'červen')"><xsl:text>06</xsl:text></xsl:when>
            <xsl:when test="contains(.,'červenec')"><xsl:text>07</xsl:text></xsl:when>
            <xsl:when test="contains(.,'srpen')"><xsl:text>08</xsl:text></xsl:when>
            <xsl:when test="contains(.,'září')"><xsl:text>09</xsl:text></xsl:when>
            <xsl:when test="contains(.,'říjen')"><xsl:text>10</xsl:text></xsl:when>
            <xsl:when test="contains(.,'listopad')"><xsl:text>11</xsl:text></xsl:when>
            <xsl:when test="contains(.,'prosinec')"><xsl:text>12</xsl:text></xsl:when>
        </xsl:choose>
    </xsl:template>

<!--  Gets day of the month  -->
    <xsl:template match="//tr/td[1]">
        <xsl:variable name="day" select="substring-before(.,'/')"/>
        <xsl:choose>
            <xsl:when test="string-length($day)!=2"><xsl:text>0</xsl:text></xsl:when>
        </xsl:choose>
        <xsl:value-of select="$day"/>
    </xsl:template>

</xsl:stylesheet>
